var globalError = new Array();

function displayErrorMessage(message){
	var errorDiv = document.getElementById('errorMessage');
	var successDiv = document.getElementById('successMessage');
	if(errorDiv){
		errorDiv.innerHTML = message;
		errorDiv.style.display = '';
		successDiv.style.display = 'none';
		$('#message').slideDown(1000);
		setTimeout("$('#message').slideUp(1000);", 20000);
		scroll(0,0);
	}	
}

function displaySuccessMessage(message){
	var errorDiv = document.getElementById('errorMessage');
	var successDiv = document.getElementById('successMessage');
	if(successDiv){
		successDiv.innerHTML = message;
		successDiv.style.display = '';
		errorDiv.style.display = 'none';
		$('#message').slideDown(1000);
		setTimeout("$('#message').slideUp(1000);", 20000);
		scroll(0,0);
	}	
}

function registerValidationPassword(validateId, controlId, controlIdRepeat, type, displayLabelId, messageRepeat){
	if(globalError[validateId] == undefined){
		globalError[validateId] = new Array();
	}
	var length = globalError[validateId].length; 
	globalError[validateId][length] = new Array();
	var array = globalError[validateId][length];
	array["controlId"] = controlId;
	array["controlIdRepeat"] = controlIdRepeat;
	array["type"] = type;	
	array["displayLabelId"] = displayLabelId;
	array["messageRepeat"] = messageRepeat;	
}


function registerValidation(validateId, controlId, required, type, displayLabelId, messageRequired, messageValidate, text){
	if(globalError[validateId] == undefined){
		globalError[validateId] = new Array();
	}
	var length = globalError[validateId].length; 
	globalError[validateId][length] = new Array();
	var array = globalError[validateId][length];
	array["controlId"] = controlId;
	array["type"] = type;
	array["required"] = required;
	array["displayLabelId"] = displayLabelId;
	array["messageRequired"] = messageRequired;
	array["messageValidate"] = messageValidate;
	array["text"] = text;
}

function validateSubmit(validateId){
	if(globalError[validateId] != undefined){
		var array = globalError[validateId];
		var length = globalError[validateId].length;
		
		for (i = 0; i<length;i++){
			var values = array[i];
			var type = values["type"];
			var text = document.getElementById(values["controlId"]).value;
			var validate = true;
			switch (type){
				case "string":
					validate = validate_string(text,values["required"],values["displayLabelId"],values["messageRequired"], values["text"]);
					break;
				case "number":
					validate = validate_number(text,values["required"],values["displayLabelId"],values["messageRequired"],values["messageValidate"], values["text"]);
					break;
				case "url":
					validate = validate_url(text,values["required"],values["displayLabelId"],values["messageRequired"],values["messageValidate"], values["text"]);
					break;	
				case "date":
					validate = validate_date(values["controlId"],values["required"],values["displayLabelId"],values["messageRequired"],values["messageValidate"], values["text"]);
					break;
				case "password":
					var repeatText = document.getElementById(values["controlIdRepeat"]).value;
					validate = validate_password(text,repeatText,values["displayLabelId"],values["messageRepeat"]);
					break;
				case "email":
					validate = validate_email(text,values["required"],values["displayLabelId"],values["messageRequired"],values["messageValidate"], values["text"]);
					break;
				case "select":
					validate = validate_select(values["controlId"],values["required"],values["displayLabelId"],values["messageRequired"]);
					break;	
			}
			if(!validate){
				displayErrorMessage(language["generic:error:message"]);
				return false;
			}	
		}
	}
	
	return true;
}

function validate_select(controlId, required, displayLabelId, messageRequired){
	var displayLabel = document.getElementById(displayLabelId);
	var control = document.getElementById(controlId);
	if(required && control.selectedIndex == 0){		
		displayLabel.innerHTML= messageRequired;
		displayLabel.style.display = '';
		return false;
	}
	
	displayLabel.style.display = 'none';
	return true;	
}


function validate_password(string, stringRepeat, displayLabelId, messageRepeat){
	var displayLabel = document.getElementById(displayLabelId);

	if(string != stringRepeat){		
		displayLabel.innerHTML= messageRepeat;
		displayLabel.style.display = '';
		return false;
	}
	
	displayLabel.style.display = 'none';
	return true;
}


function validate_string(string, required, displayLabelId, messageRequired, emptyText){
	var displayLabel = document.getElementById(displayLabelId);		
	if(required && (string == emptyText || string == '')){		
		displayLabel.innerHTML= messageRequired;
		displayLabel.style.display = '';
		return false;
	}
	
	displayLabel.style.display = 'none';
	return true;
	
}

function validate_number(string, required, displayLabelId, messageRequired, messageValidate, emptyText){
	var displayLabel = document.getElementById(displayLabelId);		
	if(required && (string == emptyText || string == '')){		
		displayLabel.innerHTML= messageRequired;
		displayLabel.style.display = '';
		return false;
	}	
	
	if (!(string == emptyText || string == '') && isNaN(string)){		
		displayLabel.innerHTML= messageValidate;
		displayLabel.style.display = '';		
		return false;
	}
	
	displayLabel.style.display = 'none';
	return true;
		
}

function validate_email(string, required, displayLabelId, messageRequired, messageValidate, emptyText){
	var displayLabel = document.getElementById(displayLabelId);		
	if(required && (string == emptyText || string == '')){		
		displayLabel.innerHTML= messageRequired;
		displayLabel.style.display = '';
		return false;
	}	
	
	if (!(string == emptyText || string == '') && !isEmail(string)){		
		displayLabel.innerHTML= messageValidate;
		displayLabel.style.display = '';		
		return false;
	}
	
	displayLabel.style.display = 'none';
	return true;
		
}

function isEmail(string) {
	if (/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(string)){
	   return true;
	  } else {
	   return false;
	  }
	}

function validate_url(string, required, displayLabelId, messageRequired, messageValidate, emptyText){
	var displayLabel = document.getElementById(displayLabelId);		
	if(required && (string == emptyText || string == '')){				
		displayLabel.innerHTML= messageRequired;
		displayLabel.style.display = '';
		return false;
	}	
	
	if (!(string == emptyText || string == '') && !isUrl(string)){		
		displayLabel.innerHTML= messageValidate;
		displayLabel.style.display = '';		
		return false;
	}
	
	displayLabel.style.display = 'none';
	return true;
		
}

function validate_date(controlId, required, displayLabelId, messageRequired, messageValidate, emptyText){
	var control = document.getElementById(controlId);
	var displayLabel = document.getElementById(displayLabelId);		
	if(required && (control.value== emptyText || control.value == '')){		
		displayLabel.innerHTML= messageRequired;
		displayLabel.style.display = '';
		return false;
	}	
	
	if(control.value== emptyText || control.value == ''){
		return true;
	}
	
	if (!isDate(control.value)){		
		displayLabel.innerHTML= messageValidate;
		displayLabel.style.display = '';		
		return false;
	}
	
	displayLabel.style.display = 'none';
	return true;
		
}



var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){	
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var pos3=dtStr.length
	
	if(currentLang == "en"){
		var strMonth=dtStr.substring(0,pos1)
		var strDay=dtStr.substring(pos1+1,pos2)
		var strYear=dtStr.substring(pos2+1, pos3)
	}else if (currentLang == "es"){
		var strDay=dtStr.substring(0,pos1)
		var strMonth=dtStr.substring(pos1+1,pos2)
		var strYear=dtStr.substring(pos2+1,pos3)
	}
	
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){		
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){		
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){		
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){		
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 ){		
		return false
	}
	return true
}


function cleanValue(id, text){
	var control = document.getElementById(id);
	if(control.value == text){
		control.value = '';
	}
}

function setValue(id, text){
	var control = document.getElementById(id);
	if(control.value == ''){
		control.value = text;
	}
}

function emtpyValue(id){
	var control = document.getElementById(id);
	control.value = '';
}


function isUrl(url) { 
	var regex=/^(ht|f)tps?:\/\/\w+([\.\-\w]+)?\.([a-z]{2,4}|travel)(:\d{2,5})?(\/.*)?$/i;
	if (!regex.test(url)){ 
		 regex=/^\w+([\.\-\w]+)?\.([a-z]{2,4}|travel)(:\d{2,5})?(\/.*)?$/i;
		 return regex.test(url); 
	}
	return regex.test(url); 
}
function validateURL(url){
	return isUrl(url);
}

function showHide(id){
	$("#" + id ).toggle();
}

function showPopup(id){
	document.getElementById(id).style.display = "";	
}

function hidePopup(id){
	document.getElementById(id).style.display = "none";	
}

function loadPopup(url){
	showPopup('popup');
	$('#popupContent').html("<div class='loading'><img src='" + wwwroot + "images/loading.gif' alt='' /></div>").load(url);
}

function getPosition(obj){
    var topValue= 0,leftValue= 0;            
    while(obj){
        leftValue+= obj.offsetLeft;
        topValue+= obj.offsetTop;
        obj= obj.offsetParent;
        if(obj){
	        var jqueryObject = $('#' + obj.id);
	        if(obj != null && (jqueryObject.css("position") == 'absolute' || jqueryObject.css("position") == 'relative' )){
	        	obj = null;
	        }
    	}    
    }            
    var array = new Array();            
    array[0] = leftValue;
    array[1] = topValue;            
    return array;
}

function trim (string) {
	return string.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

document.getElementsByClassName = function(cl) {
	var retnode = [];
	var myclass = new RegExp('\\b'+cl+'\\b');
	var elem = this.getElementsByTagName('*');
	for (var i = 0; i < elem.length; i++) {
	var classes = elem[i].className;
	if (myclass.test(classes)) retnode.push(elem[i]);
	}
	return retnode;
	};
	

function deleteConfirmation(message){
	return confirm(message);
}